package com.situ.layuimini.interceptor;

import com.situ.layuimini.entity.User;
import com.situ.layuimini.exception.NoLoginException;
import com.situ.layuimini.util.TokenUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String token = request.getHeader("Authorization");
        User u;
        if (token == null || token.equals("")) {//token没传，直接返回
            u = null;
        } else {
            u = TokenUtil.parse(token);
        }
        if (u == null) {
            throw new NoLoginException();
        }
        return true;
    }
}
